<?php

namespace App\Http\Services;

use App\Models\Banner;
use App\Models\Category;
use App\Models\Insure;

class IndexService
{
    public function banners()
    {
        $list = Banner::where([])->orderBy('sort')->get();
        foreach ($list as $k => $v) {
            $list[$k]->image = env('APP_URL') . '/storage/' . $v->image;
        }
        return $list;
    }

    public function category($is_recommend)
    {
        $where = [];
        if ($is_recommend == 1) {
            $where[] = ['is_recommend', '=', 1];
        }
        $list = Category::where($where)->orderBy('sort')->get();
        foreach ($list as $k => $v) {
            $list[$k]->icon = env('APP_URL') . '/storage/' . $v->icon;
        }
        return $list;
    }

    public function product($param)
    {
        $pagesize = (int)$param['pagesize']??20;
        $where = [];
        if (isset($param['is_recommend']) && $param['is_recommend'] == 1) {
            $where[] = ['is_recommend', '=', 1];
        }
        if (isset($param['cid']) && $param['cid'] > 0) {
            $where[] = ['cid', '=', $param['cid']];
        }
        if (isset($param['keyword'])) {
            $where[] = ['title', 'like', '%'.$param['keyword'].'%'];
        }

        $list = Insure::where($where)->orderBy('sort')->paginate($pagesize);
        foreach ($list as $k => $v) {
            $list[$k]->image = env('APP_URL') . '/storage/' . $v->image;
        }
        return $list;
    }
}
